package com.af.dao.mobile.impl;

import java.sql.SQLException;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;

import com.af.common.CommonUtil;
import com.af.common.DBHelper;
import com.af.common.Log4jXmlConfig;
import com.af.common.PropertiesManager;
import com.af.dao.mobile.IMobileNotice;
import com.af.dao.pay.IPayToApi;
import com.af.dao.pay.PayEntity;
import com.af.dao.pay.impl.PayToApi;
import com.af.entity.mobile.MobilePar;
import com.af.entity.mobile.Order;

public class MobileNotice implements IMobileNotice {

	Logger LOG = Log4jXmlConfig.getInstance().returnLogger(MobileNotice.class);

	@Override
	public void getMobileNotice(HttpServletRequest request) {
		// TODO Auto-generated method stub
		Map map = CommonUtil.returnParamMap(request);

		int status = Integer.valueOf(map.get("status").toString());

		Order order = searchOrder(map.get("orderid").toString());

		// 实际完成金额
		order
				.setRealPrice(Double.parseDouble(map.get("ordermoney")
						.toString()));

		order.setLastState(status);

		addNewNotice(map);
		
		switch (status) {
		// 充值成功
		case 2:

			success(order);

			break;
		// 部分成功
		case 3:
			partialSuccess(order);

			break;
		// 充值失败
		case 4:
			failure(order);

			break;

		}
	}

	/**
	 * 获取存在的订单数据信息
	 */
	public Order searchOrder(String orderid) {

		DBHelper dh = DBHelper.getInstance();

		dh.setBeanClass(Order.class);

		Order order = null;

		try {

			order = (Order) dh
					.query("SELECT * FROM Mobile_Record WHERE orderId='"
							+ orderid + "'");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		dh.close();
		return order;
	}

	/**
	 * 添加新通知
	 * 
	 * @param map
	 */
	public void addNewNotice(Map map) {
		String sql = "{call }";

		Object[] args = new Object[4];

		args[0] = map.get("");

		args[1] = map.get("");

		args[2] = map.get("");

		args[3] = map.get("");

		update(sql, args);
	}

	/**
	 * 充值成功
	 */
	public void success(Order order) {
		IPayToApi aliPay = new PayToApi();

		PayEntity pe = new PayEntity();

		pe.setTot_trans_amt(new String().valueOf(order.getSalePrice() * 100));

		pe.setOrderid(order.getOrderid());

		pe.setBuyer_id(order.getMemberNo());

		pe.setSub_order_flag("N");

		pe.setSub_order_num(0);

		pe.setOrder_desc("");

		pe.setNotify_url("");

		aliPay.YSQWC(pe);

	}

	/**
	 * 部分成功订单
	 */
	public void partialSuccess(Order order) {
		IPayToApi aliPay = new PayToApi();

		double salePrice = returnSalePriceForPay(order);

		order.setSalePrice(salePrice);

		MobilePar mp = new MobilePar(order.getOrderid(), order.getRealPrice(),
				salePrice);

		addMobilePar(mp);

		PayEntity pe = new PayEntity();

		pe.setTot_trans_amt(new String().valueOf(salePrice * 100));

		pe.setOrderid(order.getOrderid());

		pe.setBuyer_id(order.getMemberNo());

		pe.setSub_order_flag("N");

		pe.setSub_order_num(0);

		pe.setOrder_desc("");

		pe.setNotify_url(PropertiesManager.getInstance().returnValue(
				"MOBILE_NOTICE"));

		aliPay.YSQWC(pe);
	}

	/**
	 * 充值失败
	 * 
	 * @param strSql
	 */
	public void failure(Order order) {
		IPayToApi aliPay = new PayToApi();

		PayEntity pe = new PayEntity();

		pe.setOrderid(order.getOrderid());

		pe.setTot_trans_amt(new String()
				.valueOf((int) order.getSalePrice() * 100));

		pe.setSub_order_flag("N");

		pe.setSub_order_num(0);

		pe.setBuyer_id(order.getMemberNo());

		pe.setOrder_desc("");

		pe.setNotify_url(PropertiesManager.getInstance().returnValue(
				"MOBILE_NOTICE"));

		aliPay.YSQCX(pe);

	}

	private int update(String strSql, Object... args) {
		
		int count=0;
		
		DBHelper dh = DBHelper.getInstance();

		try {

			count=dh.update(strSql, args);
			
			dh.close();

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			LOG.error(new StringBuffer("操作数据时异常").append(e.getMessage())
					.append("\n").append("执行语句：").append(strSql).append("\n")
					.append("参数信息：").append(args));
			
			LOG.error(strSql);
			
			e.printStackTrace();
		}finally{
			
			return count;
			
		}

	}

	/**
	 * 添加部分充值记录
	 * 
	 * @param mp
	 */
	private void addMobilePar(MobilePar mp) {
		String sql = "{call addMobilePar(?,?,?)}";

		Object[] args = new Object[3];

		args[0] = mp.getOrderid();

		args[1] = mp.getOrdermoney();

		args[2] = mp.getSalePrice();

		update(sql, args);
		
		//修改订单状态
	}

	/**
	 * 返回部份充值应支付金额
	 * 
	 * @param order
	 * @return
	 */
	private double returnSalePriceForPay(Order order) {
		return (float) ((int) ((float) order.getSalePrice()
				/ order.getProPrice() * order.getRealPrice() * 100)) / 100;
	}
}
